package cn.jetpiece.cloud.joss.sdk.core.oss;

import cn.jetpiece.cloud.joss.sdk.core.oss.model.CreateBucket;
import cn.jetpiece.cloud.joss.sdk.core.util.RestUtils;
import com.alibaba.fastjson.JSONObject;
import cn.jetpiece.cloud.joss.sdk.core.oss.model.OssSdkBaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;

/**
 * @author XuJZ
 * @date 2021/1/28
 */
public class BucketService {
    private RestUtils restUtils;
    private final Logger log = LoggerFactory.getLogger(BucketService.class);

    public void setRestUtils(RestUtils restUtils) {
        this.restUtils = restUtils;
    }

    /**
     * @param bucketName         桶名
     * @param providerId         服务商id
     * @param regionId           域id
     * @param storageClassId     存储类型id
     * @param dataRedundancyType 冗余
     */
    public void createBucket(String bucketName, String providerId, String regionId, String storageClassId, Integer dataRedundancyType) {
        CreateBucket createBucket = new CreateBucket();
        createBucket.setBucketName(bucketName);
        createBucket.setProviderId(providerId);
        createBucket.setRegionId(regionId);
        createBucket.setStorageClassId(storageClassId);
        createBucket.setDataRedundancyType(dataRedundancyType);
        createBucket(createBucket);
    }

    /**
     * @param request 桶信息
     */
    public void createBucket(CreateBucket request) {
        Assert.notNull(request.getBucketName(), "桶不能为空！");
        Assert.notNull(request.getProviderId(), "服务商id不能为空！");
        Assert.notNull(request.getRegionId(), "域id不能为空！");
        Assert.notNull(request.getStorageClassId(), "存储类型id不能为空！");
        OssSdkBaseResult<String> baseResult = restUtils.exchange("/oss/buckets", HttpMethod.POST, request, String.class);
        log.info(JSONObject.toJSONString(baseResult));
    }

    /**
     *
     */
    public void getBucket() {
        OssSdkBaseResult<String> baseResult = restUtils.exchange("/oss/buckets", HttpMethod.GET, String.class);
        log.info(JSONObject.toJSONString(baseResult));
    }

}
